#include "sms.h"

IconListView *iconlist_view_new(GooCanvasItem *parent, gdouble x, gdouble y,
		                                                 GdkPixbuf *pixbuf_bg, GdkPixbuf *pixbuf_showarea,
		                                                 gdouble icon_w, gdouble icon_h,
		                                                 gdouble h_margins, gdouble w_margins,
		                                                 gdouble h_spacing, gdouble w_spacing,
		                                                 int total_icon, 
		                                                 Theme *theme) {
	gdouble frame_x, frame_y, frame_w, frame_h;
	gdouble showarea_x, showarea_y, showarea_w, showarea_h;
	
	IconListView *ilv;
	ilv = (IconListView *) g_malloc0(sizeof(IconListView) + 1);
	ilv->parent=parent;
	ilv->x=x;
	ilv->y=y;
	ilv->icontextlist = NULL;
	
	ilv->w = gdk_pixbuf_get_width(pixbuf_bg);
	ilv->h = gdk_pixbuf_get_height(pixbuf_bg);
	ilv->cavas = goo_canvas_group_new(parent, "x", ilv->x,
	                                          "y", ilv->y,
	                                          "width", ilv->w,
	                                          "height", ilv->h, NULL);;
	ilv->background = goo_canvas_image_new (ilv->cavas, pixbuf_bg,
                                                        0, 0, "scale-to-fit", TRUE, NULL);
		
	ilv->total_icon = total_icon;
	ilv->cur_page = 1;
	ilv->total_page = 0;
	ilv->theme = theme;
	
	ilv->h_margins = h_margins,
	ilv->w_margins = w_margins;
	ilv->h_spacing = h_spacing;
	ilv->w_spacing = w_spacing;
	
	frame_x = w_margins;
	frame_y = h_margins;
	frame_w = ilv->w - w_margins*2;
	
	ilv->frame_column_num = (frame_w + w_spacing) / (icon_w + w_spacing);
	if (total_icon%(ilv->frame_column_num))	{
		ilv->frame_row_num = 1 + total_icon / ilv->frame_column_num;
	} else {
		ilv->frame_row_num = total_icon / ilv->frame_column_num;
	}
	frame_h = ilv->frame_row_num * icon_h + (ilv->frame_row_num - 1)* h_spacing;
	
	showarea_x = frame_x;
	showarea_y = frame_y;
	showarea_w = gdk_pixbuf_get_width(pixbuf_showarea);
	showarea_h = gdk_pixbuf_get_height(pixbuf_showarea);
	
	ilv->show_column_num = ilv->frame_column_num;
	ilv->show_row_num = (showarea_h + h_spacing) / (icon_h + h_spacing); 
	ilv->showarea = goo_canvas_group_new(ilv->cavas, "x", showarea_x,
                                                   "y", showarea_y,
                                                   "width", showarea_w,
                                                   "height", showarea_h, NULL);
	
	ilv->frame = goo_canvas_group_new(ilv->showarea, "x", 0,
                                                    "y", 0,
                                                    "width", showarea_w,
                                                    "height", frame_h, NULL);

	
	return ilv;
}

IconListView *iconlist_view_new_by_theme(GooCanvasItem *parent, gdouble x, gdouble y, Theme *theme) {
	IconListView *ilv;
	ilv = (IconListView *) g_malloc0(sizeof(IconListView) + 1);
	return ilv;
}

void iconlist_view_destroy(IconListView *ilv) {
	
}

void iconlist_view_free_icontextlist(IconListView *ilv) {
	
}

IconTextView *iconlist_view_create_icontextview(IconListView *ilv, GdkPixbuf *pixbuf_bg, GdkPixbuf *pixbuf_fg, GdkPixbuf *pixbuf_icon,
                                                                   gdouble icon_x, gdouble icon_y,
                                                                   gdouble text_x, gdouble text_y, gdouble text_w, gchar *text_str,
                                                                   Theme *theme, int id) {
    int x, y;
    int column, row;
    row = id/ilv->frame_column_num;
    column = id%ilv->frame_column_num;
    x = column * (icon_x + w_spacing);
    y = row * (icon_y + h_spcing);
    printf("x:%d y:%d\n", x, y);
	return icontext_view_new(ilv->frame, x, y,
			                      pixbuf_bg, pixbuf_fg, pixbuf_icon,
							      icon_x, icon_y,
							      text_x, text_y, text_w, text_str,
							      theme);
}

gboolean iconlist_view_set_icontextlist(IconListView *ilv, IconTextView **icontextlist) {
	if (ilv == NULL)
		return FALSE;
	if (ilv->icontextlist) {
		iconlist_view_free_icontextlist(ilv);
	}
	ilv->icontextlist = icontextlist;
}

gboolean iconlist_view_verial_scroll_list(IconListView *itv, int direct) {
	
	return TRUE;
}

gboolean iconlist_view_show(IconListView *ilv) {
	return TRUE;
}
